package com.buka.book.service;

import com.buka.book.dao.BookDao;
import com.buka.book.entity.Book;

import java.util.List;

public class BookService {

    BookDao bookDao = new BookDao();

    public List<Book> bookList() {
        return bookDao.bookList();
    }

    public Book getById(int id) {
        return bookDao.queryBookById(id);
    }

    public int updateBook(Book book) {
        return bookDao.updateBook(book);
    }


    public int save(Book book) {

        //根据书籍编号查询是否存在记录
        Book book_1 = bookDao.queryBookByNo(book.getBookNo());
        if (null == book_1) {
            //没有查到记录，直接新增
            return bookDao.add(book);
        } else {
            //查到记录，则修改，数量累加
            int count = book_1.getCount();
            book_1.setCount(count + book.getCount());

            return bookDao.updateBook(book_1);
        }
    }
}
